﻿using System.Linq;
using System.Web.Mvc;
using DevelopmentKit.Cms.Domain;
using DevelopmentKit.Extensions.SharpArch.Repositorys;

namespace DevelopmentKit.Cms.Web.Controllers
{
    public class ContentController : Controller
    {

        private readonly IENSRepository<Content> _repository;

        public ContentController(IENSRepository<Content> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取首页页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("Root")]
        public ActionResult Root(string id)
        {
            var content =
                _repository.FindAll().FirstOrDefault(x => x.Definition.IsRoot && x.Definition.EnterpriseId == Current.EnterpriseId);

           return Result(content);
        }


        /// <summary>
        /// 获取栏目页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Part(string greatgrand, string grand, string parent, string part)
        {
            if (!string.IsNullOrEmpty(parent))
            {
                part = parent + "/" + part;
            }
            if (!string.IsNullOrEmpty(grand))
            {
                part = grand + "/" + part;
            }
            if (!string.IsNullOrEmpty(greatgrand))
            {
                part = greatgrand + "/" + part;
            }
            var content =
                _repository.FindAll().FirstOrDefault(x => x.Name == part && x.Definition.EnterpriseId == Current.EnterpriseId);
 
           return Result(content);
        }
        /// <summary>
        /// 获取内容页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Detail(string greatgrand, string grand, string parent, string part, string id)
        {
            if (!string.IsNullOrEmpty(parent))
            {
                part = parent + "/" + part;
            }
            if (!string.IsNullOrEmpty(grand))
            {
                part = grand + "/" + part;
            }
            if (!string.IsNullOrEmpty(greatgrand))
            {
                part = greatgrand + "/" + part;
            }
            if (!string.IsNullOrEmpty(part))
            {
                id = part + "/" + id;
            }
            var content =
                _repository.FindAll().FirstOrDefault(x => x.Name == id && x.Definition.EnterpriseId == Current.EnterpriseId);
 
           return Result(content);
        }
        /// <summary>
        /// 返回当前的页面
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private ActionResult Result(Content content)
        {
            return View(content.Definition.Template.Path, content);
        }
    }
}
